#include <string.h>

#include "bt.h"
#include "bt_express.h"

void test_express(const char * express)
{
	int ret = 0;

	const char * e = express;
	bt_node_t * bt = 0;
	if ((ret = express2bt_r(&e, &bt))) {
		xerr("express2bt_r return %d", ret);
		return;
	}

	char buff[4096];
	size_t buff_len = sizeof(buff);
	memset(buff, 0, buff_len);
	bt2express_r(bt, buff, &buff_len);

	xlog("\n------------------------------\n");
	xlog("%s\n%s", express, buff);
	xlog("\n------------------------------\n");
}

int bt_node_hook(bt_node_t * node)
{
	xlog(" %c", node->value);
	return 0;
}

const char * express_array[] =
{
		"(a,(b,(),()),(c,(),()))",
		"(a,(b,(x,(),()),()),(c,(),()))",

		"(A,(B,(D,(),()),()),(C,(X,(Y,(),()),()),(Z,(),())))",
		0
};

int main(int argc, char ** argv)
{
#if 0
	const char * express = "(A,(B,(D,(),()),()),(C,(X,(Y,(),()),()),(Z,(),())))";

	test_express(express);

	bt_node_t * bt = 0;
	if (express2bt_r(&express, &bt)) {
		xerr("express2bt_r error! \n");
		return -1;
	}
#else
	(void)test_express;
#endif

	const char ** e = express_array;
	for (; *e; ++e) {

		xlog("\n\n : %s", *e);
		bt_node_t * bt = 0;
		if (express2bt_r(e, &bt)) {
			xerr("express2bt_r error! \n");
			return -1;
		}

		xlog("\n bt_for_each_pre_order  --> ");
		bt_for_each_pre_order(bt, bt_node_hook);

		xlog("\n bt_for_each_in_order   --> ");
		bt_for_each_in_order(bt, bt_node_hook);

		xlog("\n bt_for_each_post_order --> ");
		bt_for_each_post_order_x(bt, bt_node_hook);
	}

	return 0;
}

